package ncut.shumei;

import ncut.shumei.dao.BookDao;
import ncut.shumei.entity.Book;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        try {
            ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
            BookDao bookDao = context.getBean("bookDao", BookDao.class);

            System.out.println("\n=== 1. 测试按ISBN查询 ===");
            Book book1 = bookDao.findByIsbn("123456");
            System.out.println(book1);

            System.out.println("\n=== 2. 测试按书名查询 ===");
            Book book2 = bookDao.findByName("计算机网络教程");
            System.out.println(book2);

            System.out.println("\n=== 3. 测试模糊查询(包含'Web'的图书) ===");
            List<Book> webBooks = bookDao.findByNameContainingWeb("Web");
            webBooks.forEach(System.out::println);






            System.out.println("\n=== 4. 测试添加图书 ===");
            Book newBook = new Book();
            newBook.setIsbn("6666668888");
            newBook.setName("Web开发入门");
            newBook.setPrice(45.0);
            newBook.setAuthor("测试作者");
            newBook.setPress("测试出版社");
            bookDao.addBook(newBook);

            System.out.println("\n=== 5. 测试修改图书 ===");
            newBook.setPrice(50.0);
            newBook.setAuthor("更新后的作者");
            bookDao.updateBook(newBook);


            System.out.println("\n=== 6. 测试删除图书 ===");
            bookDao.deleteBook("6666668888");



            System.out.println("\n=== 7. 测试查询借阅学生 ===");
            List<String> students = bookDao.findStudentsByBookName("计算机网络教程");
            System.out.println("借阅《计算机网络教程》的学生：");
            students.forEach(student -> System.out.println("- " + student));

        } catch (Exception e) {
            System.err.println("程序运行出错: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

